import { faker } from '@faker-js/faker';
import { Tournament } from 'src/modules/tournament/entities/tournament.entity';
import { Category } from 'src/modules/category/entities/category.entity';
import { Gymnasium } from 'src/modules/gymnasium/entities/gymnasium.entity';
import { Event } from 'src/modules/event/entities/event.entity';
import { EventTypeEnum } from 'src/common/constant/enums';
import { EventToPlayer } from 'src/modules/event-to-player/entities/event-to-player.entity';
// import { Team } from 'src/modules/team/entities/team.entity';
import { User } from 'src/modules/user/entities/user.entity';
import { PlayerSignup } from 'src/modules/player-signup/entities/player-signup.entity';

export function getMockTournament(param: {
  id: number;
  name?: string;
  categories?: Category[];
  gymnasiums?: Gymnasium[];
}): Tournament {
  const { id, name, categories, gymnasiums } = param;
  const tournament = {} as Tournament;

  tournament.id = id;
  tournament.name = name || faker.company.name() + ' 羽毛球杯';
  tournament.address = faker.location.streetAddress();
  tournament.fromDate = faker.date.future();
  tournament.endDate = faker.date.future({ refDate: tournament.fromDate });
  tournament.type = faker.number.int({ min: 1, max: 5 });
  tournament.registrationFromDate = faker.date.past();
  tournament.registrationEndDate = faker.date.between({
    from: tournament.registrationFromDate,
    to: tournament.fromDate,
  });
  tournament.status = faker.number.int({ min: 0, max: 3 });
  tournament.adImage = faker.image.url();
  tournament.detailImage = faker.image.url();
  tournament.createBy = faker.number.int({ min: 1, max: 100 });
  tournament.updateBy = faker.number.int({ min: 1, max: 100 });

  // 使用传入的 categories 和 gymnasiums
  tournament.categories = categories;
  tournament.gymnasiums = gymnasiums;

  return tournament;
}
export function getMockCategory(param: {
  id: number;
  name?: string;
  events?: Event[];
}): Category {
  const { id, events, name } = param;
  const category = new Category();

  category.id = id;
  category.name = name || faker.lorem.words(2);
  category.alias = faker.lorem.word();
  category.minAge = faker.number.int({ min: 5, max: 15 });
  category.maxAge = faker.number.int({ min: category.minAge + 1, max: 30 });
  category.createBy = faker.number.int({ min: 1, max: 100 });
  category.createTime = faker.date.past();
  category.updateBy = faker.number.int({ min: 1, max: 100 });
  category.updateTime = faker.date.recent();

  // 生成指定数量的模拟 Event 数据
  category.events = events;

  // 注意：我们不设置 tournament 字段，因为它是 ManyToOne 关系

  return category;
}

export function getMockEvent(param: {
  id: number;
  name?: string;
  eventToPlayers: EventToPlayer[];
}): Event {
  const { id, name, eventToPlayers } = param;
  const event = new Event();

  event.id = id;
  event.name = name || faker.lorem.words(3);
  event.type = faker.helpers.arrayElement(
    Object.values(EventTypeEnum) as number[],
  );
  event.fee = faker.number.float({ min: 0, max: 1000 });
  event.minPlayerCount = faker.number.int({ min: 1, max: 10 });
  event.maxPlayerCount = faker.number.int({
    min: event.minPlayerCount,
    max: 100,
  });
  event.createBy = faker.number.int({ min: 1, max: 100 });
  event.createTime = faker.date.past();
  event.updateBy = faker.number.int({ min: 1, max: 100 });
  event.updateTime = faker.date.recent();
  event.eventToPlayers = eventToPlayers;

  return event;
}

export function getMockEventToPlayer(param: {
  id: number;
  registrationIndex?: string;
  player?: PlayerSignup;
  player2?: PlayerSignup;
  // team?: Team;
}): EventToPlayer {
  const eventToPlayer = new EventToPlayer();
  const { id, registrationIndex, player, player2 } = param;

  eventToPlayer.id = id;
  eventToPlayer.registrationIndex =
    registrationIndex || faker.number.int({ min: 1, max: 10 }).toString();

  // 模拟 User 实体 (player)
  eventToPlayer.player = player;
  eventToPlayer.player2 = player2;
  // eventToPlayer.team = team;

  // 随机选择一个 EventTypeEnum 值
  // if (eventToPlayer.team) {
  //   const eventTypes = Object.values(EventTypeEnum);
  //   eventToPlayer.subTeamMatchType = faker.helpers.arrayElement(
  //     eventTypes,
  //   ) as EventTypeEnum;
  // }
  return eventToPlayer;
}
